{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "using Unums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Complex{Unums.Ubound{3,4,UInt64}}\n",
       "(1.0999908447265625,1.100006103515625) + 0.0*im"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "complex(Ubound34(1.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "fftx (generic function with 1 method)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# computes the fft coefficient in high precision, then converts back\n",
    "fftx_coef{T}(::Type{T},k,N) = convert(T,exp(-2*π*im*BigFloat(k)/N)) \n",
    "\n",
    "# simple Cooley-Tukey FFT\n",
    "function fftx(x)\n",
    "    N = length(x)\n",
    "    if N == 1\n",
    "        return x\n",
    "    else\n",
    "        y1 = fftx(x[1:2:N-1])\n",
    "        y2 = fftx(x[2:2:N])\n",
    "        \n",
    "        n = length(y1)\n",
    "        X = similar(x)\n",
    "        for k = 1:n\n",
    "            a,b = y1[k], y2[k]\n",
    "            z = fftx_coef(eltype(x),k-1,N)\n",
    "            X[k] = a + z * b\n",
    "            X[n+k] = a - z * b\n",
    "        end\n",
    "        return X\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8-element Array{Complex{Unums.Ubound{3,4,UInt64}},1}:\n",
       "    (0.802093505859375,0.8021011352539062)+0.0*im\n",
       "  (0.4930763244628906,0.49308013916015625)+0.0*im\n",
       "     (0.55255126953125,0.5525588989257812)+0.0*im\n",
       "    (0.573211669921875,0.5732192993164062)+0.0*im\n",
       "  (0.2057971954345703,0.20579910278320312)+0.0*im\n",
       "  (0.35410308837890625,0.3541069030761719)+0.0*im\n",
       "  (-0.827728271484375,-0.8277206420898438)+0.0*im\n",
       " (-0.9381637573242188,-0.9381561279296875)+0.0*im"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = randn(8)\n",
    "xc = complex(x)\n",
    "u = map(Ubound34,x)\n",
    "uc = complex(u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8-element Array{Complex{Float64},1}:\n",
       "   1.21498+0.0im     \n",
       " -0.374135-2.54725im \n",
       "   1.28306-1.21213im \n",
       "   1.56674+0.213309im\n",
       "  0.250488+0.0im     \n",
       "   1.56674-0.213309im\n",
       "   1.28306+1.21213im \n",
       " -0.374135+2.54725im "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fft(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8-element Array{Complex{Float64},1}:\n",
       "   1.21498+0.0im     \n",
       " -0.374135-2.54725im \n",
       "   1.28306-1.21213im \n",
       "   1.56674+0.213309im\n",
       "  0.250488+0.0im     \n",
       "   1.56674-0.213309im\n",
       "   1.28306+1.21213im \n",
       " -0.374135+2.54725im "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fftx(xc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8-element Array{Complex{Unums.Ubound{3,4,UInt64}},1}:\n",
       "                         (1.214935302734375,1.214996337890625)+0.0*im                                  \n",
       " (-0.374176025390625,-0.37407684326171875)+(-2.54730224609375,-2.547149658203125)*im                   \n",
       "  (1.283050537109375,1.2830963134765625)+(-1.2121429443359375,-1.21209716796875)*im                    \n",
       "                          (1.5666656494140625,1.5667724609375)+(0.2132415771484375,0.21337890625)*im   \n",
       "                         (0.250457763671875,0.250518798828125)+0.0*im                                  \n",
       "       (1.566680908203125,1.5667877197265625)+(-0.21337890625,-0.2132415771484375)*im                  \n",
       "                          (1.2830352783203125,1.2830810546875)+(1.21209716796875,1.2121429443359375)*im\n",
       "                      (-0.37418365478515625,-0.37408447265625)+(2.547149658203125,2.54730224609375)*im "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fftx(uc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.4.3",
   "language": "julia",
   "name": "julia-0.4"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
